//
//  UITestResultsBar.m
//  WiteBox
//
//  Created by John Blanco on 1/20/10.
//  Copyright 2010 Effective UI. All rights reserved.
//

#import "UITestResultsBar.h"
#import "WiteBoxStyles.h"

@implementation UITestResultsBar

@synthesize value;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGContextClearRect(ctx, rect);

	if (value < 0.0001) {
		// fill it all green
		CGContextSetRGBFillColor(ctx, 0, 180, 0, 1.0);
		CGContextFillRect(ctx, rect);
	} else {
		CGRect greenRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width * value, rect.size.height);
		CGRect redRect = CGRectMake(rect.origin.x + greenRect.size.width, rect.origin.y, rect.size.width - greenRect.size.width, rect.size.height);

		UIColor *greenColor = [WiteBoxStyles greenCorrect];
		UIColor *redColor = [WiteBoxStyles redWrong];
		
		// fill value portion green
 		CGContextSetFillColorWithColor(ctx, greenColor.CGColor);
		CGContextFillRect(ctx, greenRect);

		// fill rest red
 		CGContextSetFillColorWithColor(ctx, redColor.CGColor);
		CGContextFillRect(ctx, redRect);
	}
}

- (void)dealloc {
    [super dealloc];
}

#pragma mark -

- (void)setValue:(double)mValue {
	value = mValue;
	
	// redraw
	[self setNeedsDisplay];
}

@end
